package com.dungeon.crawl;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.Spinner;

public class DungeonCrawlActivity extends Activity implements OnClickListener{

	//Widget declarations
	ImageButton left, right, up, down, action;
	Button stats;
	Spinner inventory;
	DungeonView view;
	//boolean to check for combat after movement//
	static boolean combat;
	static //thread to constantly check for combat
	Thread combatThread;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		//Widget setup
		stats = (Button) findViewById(R.id.buttonStats);
		stats.setOnClickListener(this);
		stats.setText("Attributes");
		left = (ImageButton) findViewById(R.id.ImageButtonleft);
		left.setOnClickListener(this);
		left.setImageResource(R.drawable.left_arrow);
		right = (ImageButton) findViewById(R.id.ImageButtonright);
		right.setOnClickListener(this);
		right.setImageResource(R.drawable.right_arrow);
		up = (ImageButton) findViewById(R.id.ImageButtonup);
		up.setOnClickListener(this);
		up.setImageResource(R.drawable.up_arrow);
		down = (ImageButton) findViewById(R.id.ImageButtondown);
		down.setOnClickListener(this);
		down.setImageResource(R.drawable.down_arrow);
		action = (ImageButton) findViewById(R.id.ImageButtoncenter);
		action.setOnClickListener(this);
		action.setImageResource(R.drawable.hand);
		inventory = (Spinner) findViewById(R.id.spinner1);

		//setup the primary view window
		FrameLayout layout = (FrameLayout) findViewById(R.id.frameLayout1);
		view = new DungeonView(this);
		layout.addView(view);
		
		//thread to constantly check for combat
		combatThread = new Thread(new Runnable() {
			public void run() {
				while(true) { // CHANGE THIS TO A TIMER!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
					//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
					synchronized(this) {
						try {
							wait(200);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
					if(combat == true) {
						startCombat();
					}
				}
			}
		});
	}
	
	public static void setCombat(boolean bool) {
		combat = bool;
		synchronized (combatThread) {
			combatThread.notifyAll();
		}
	}

	//responses to buttons clicked
	public void onClick(View v) {
		if(v.equals(up)) {
			combat = view.move(view.player, 1);
		}
		else if(v.equals(right)) {
			combat = view.move(view.player, 2);
		}
		else if(v.equals(down)) {
			combat = view.move(view.player, 3);
		}
		else if(v.equals(left)) {
			combat = view.move(view.player, 4);
		}
		synchronized (combatThread) {
			combatThread.notifyAll();
		}
	}
	
	public void startCombat() {
		Intent intents = new Intent(this, CombatActivity.class);
		this.startActivity(intents);
	}
	
	//dummy method for getting context to change activities
	protected static Context getContext() {
		return null;
	}
}